package com.zmn.oms.task.job.ordercomment;

import com.github.ltsopensource.core.domain.Action;
import com.github.ltsopensource.core.domain.Job;
import com.github.ltsopensource.spring.tasktracker.JobRunnerItem;
import com.github.ltsopensource.spring.tasktracker.LTS;
import com.github.ltsopensource.tasktracker.Result;
import com.zmn.common.utils.date.DateUtil;
import com.zmn.common.utils.number.NumberUtil;
import com.zmn.oms.manager.utils.TaskConsts;
import com.zmn.oms.task.es.business.interfaces.ordercomment.RebuildEsOrderCommentService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;

/**
 * 类描述：同步工单评价
 *
 * @author lujia
 * @date 2018/12/29 14:55
 */
@LTS
public class OrderCommentJob {

    private static final Logger logger = LoggerFactory.getLogger(OrderCommentJob.class);

    @Autowired
    RebuildEsOrderCommentService rebuildEsOrderCommentService;

    @JobRunnerItem(shardValue = TaskConsts.TASK_ID_REBUILD_ORDER_COMMENT)
    public Result runRebuildOrderWorkJob(Job job) throws Throwable {
        logger.info("执行：" + job);
        String startTimeStr = job.getParam("startTime");
        String endTimeStr = job.getParam("endTime");
        String numberStr = job.getParam("number");

        if (!DateUtil.isValid(startTimeStr, DateUtil.FORMAT_DEFAULT)) {
            return new Result(Action.EXECUTE_FAILED, "开始时间校验失败!");
        }

        if (!DateUtil.isValid(endTimeStr, DateUtil.FORMAT_DEFAULT)) {
            return new Result(Action.EXECUTE_FAILED, "结束时间校验失败!");
        }

        if (!NumberUtil.isNumber(numberStr) || Integer.valueOf(numberStr) < 1 || Integer.valueOf(numberStr) > 1000 ) {
            return new Result(Action.EXECUTE_FAILED, "数量校验失败!");
        }

        Date startTime = DateUtil.parse(startTimeStr);
        Date endTime = DateUtil.parse(endTimeStr);
        Integer number = Integer.valueOf(numberStr);
        rebuildEsOrderCommentService.rebuild(startTime, endTime, number);

        return new Result(Action.EXECUTE_SUCCESS, "执行成功了!");
    }
}
